package cn.lqs.crm.web.controller;


import cn.lqs.AjaxResult;
import cn.lqs.crm.domain.Department;
import cn.lqs.crm.service.IDepartmentService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/department")
@Api(tags = "用户接口类")
@CrossOrigin
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * restful里面的请求
     *          put delete post get patch
     *      查询所有
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 500,message = "服务器错误"),
    })
    public List<Department> findAll(){
        System.out.println("patch请求");
        return departmentService.findAll();
    }


    /**
     *      查询单条数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询用户",notes = "getOne查询方法") //value是对这个接口进行描述
    @ApiImplicitParams({
            /*name为参数名，value是参数描述，required=true是必须要传这个id，dataType参数类型 ,paramType = path就是把参数放到地址上*/
            @ApiImplicitParam(name = "id",value = "id参数",required = true,dataType = "Long",paramType = "path")
    })
    @ResponseBody
    public Department getOne(@PathVariable("id") Long id){
        System.out.println("id:" + id);
        try {
            return departmentService.findOne(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     *      新增
     * @param department
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.PUT)
    @ResponseBody
    public AjaxResult save(@RequestBody Department department){
        try {
            System.out.println("这是我接收的参数");
            System.out.println(department);
            //调用service
            departmentService.save(department);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("保存失败");
        }
        return new AjaxResult();
    }


    /**
     *      根据id删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    @CrossOrigin
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            System.out.println("你删除的id是:" + id);
            departmentService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败");
        }
        return new AjaxResult();
    }


    /**
     *      修改
     * @param department
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult update(@RequestBody Department department){
        try {
            System.out.println("修改的对象是:" + department);
            /*可以打印下接收的对象id*/
            System.out.println(department.getId());
            departmentService.update(department);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("修改失败");
        }
        return new AjaxResult();
    }



}
